﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Windows.Media;
using System.Windows.Media.TextFormatting;

namespace OpenLS.Spreadsheet.UI.Internals
{
    internal class FormulaTextRunProvider : ITextRunProvider
    {
        private RichParagraphTextRunProvider dummy;
        private readonly EditedCell _cell;
        private readonly TextRunProperties _runProperties;

        public FormulaTextRunProvider(EditedCell cell, TextRunProperties runProperties)
        {
            Contract.Requires(cell != null);
            Contract.Requires(runProperties != null);
            _cell = cell;
            _runProperties = runProperties;
        }

        private string Text
        {
            get { return _cell.Text; }
        }

        #region ITextRunProvider Members

        public IEnumerable<CellTextRun> GetRuns()
        {
            Formula formula = _cell.Formula;
            if (formula != null && formula.References != null && formula.References.Count > 0)
                return getFormulaRuns();
            return getRegularRuns();
        }

        #endregion

        private IEnumerable<CellTextRun> getRegularRuns()
        {
            if (Text.Length == 0)
                yield break;
            if (true) // if (IsFlat )
                yield return new CellTextRun(Text, _runProperties);
            else
            {
                throw new NotImplementedException();
                /*var s1 = Text.Substring(0, MinIndex);
                if (!string.IsNullOrEmpty(s1))
                    yield return new CellTextRun(s1, (_runProperties));
                s1 = Text.Substring(MinIndex, MaxIndex - MinIndex);
                yield return
                    new CellTextRun(s1, new CellInheritedTextRunProperties(_runProperties, Brushes.White, Brushes.Black))
                    ;
                s1 = Text.Substring(MaxIndex, Text.Length - MaxIndex);
                if (!string.IsNullOrEmpty(s1))
                    yield return new CellTextRun(s1, (_runProperties));
                 */
            }
            string remaining = _cell.SuggestionRemainingText;
            if (!string.IsNullOrEmpty(remaining))
                yield return
                    new CellTextRun(remaining,
                                    new CellInheritedTextRunProperties(_runProperties, Brushes.White, Brushes.Black));
        }

        private IEnumerable<CellTextRun> getFormulaRuns()
        {
            Formula formula = _cell.Formula;
            int i = 0;
            foreach (FormulaToken token in formula.Tokens)
            {
                if (token is FormulaReference)
                    yield return
                        new CellTextRun(token.Text,
                                        new CellInheritedTextRunProperties(_runProperties,
                                                                           new SolidColorBrush(getColor(i++)), null));
                else
                    yield return
                        new CellTextRun(token.Text, _runProperties);
            }
        }

        private static Color getColor(int i)
        {
            switch (i%7)
            {
                case 0:
                    return Color.FromArgb(255, 0, 0, 255);
                case 1:
                    return Color.FromArgb(255, 0, 128, 0);
                case 2:
                    return Color.FromArgb(255, 153, 0, 204);
                case 3:
                    return Color.FromArgb(255, 128, 0, 0);
                case 4:
                    return Color.FromArgb(255, 0, 204, 51);
                case 5:
                    return Color.FromArgb(255, 255, 102, 0);
                case 6:
                    return Color.FromArgb(255, 204, 0, 153);
                default:
                    throw new NotSupportedException();
            }
        }
    }
}